package com.sslite.reports.effectsofhighvolumereport;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.sslite.model.MetaData;
import com.sslite.reports.IReportRunner;

public class EffectsOfHighVolumeReportRunnerImpl implements IReportRunner{
	private List<MetaData> metaDataList;

	public EffectsOfHighVolumeReportRunnerImpl(List<MetaData> metaDataList) {
		this.metaDataList = metaDataList;
	}
	
	@Override
	public boolean generateReport() throws Exception {
		System.out.println( "EffectsOfHighVolumeReportRunnerImpl: Generating Report." );
		final ExecutorService executor = Executors.newFixedThreadPool(RPT_THREAD_POOL);
		for(final MetaData metaData : metaDataList){
			final EffectsOfHighVolumeReport ehvr = new EffectsOfHighVolumeReport(metaData);
			executor.execute( ehvr );
		}
		
		// This will make the executor accept no new threads
		// and finish all existing threads in the queue
		executor.shutdown();
		
		// Wait for 12hrs MAX until all threads are finished. If still not finished then timeout.
		boolean allReportsGenerated = executor.awaitTermination(12, TimeUnit.HOURS);
		
		return allReportsGenerated;
	}
	
}
